<template>
	<view>
		<block v-for="(item,index) in goodsList" :key="index">
			<my-goods :goods="item" @click="clickaa" ></my-goods>
		</block>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 参数对象
				params: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				goodsList: [],
				// 总页数
				total: 0,
				isLoading:false
			};
		},
		onLoad(options) {
			console.log(options)
			this.params.query = options.query || ''
			this.params.cid = options.cat_id || ''
			this.getGoodsList()
		},
		// 下拉刷新
		onPullDownRefresh() {
			// console.log(1)
			this.params.pagenum = 1
			this.goodsList=[]
			this.getGoodsList(uni.stopPullDownRefresh)
		},
		// 上拉刷新
	    onReachBottom() {
			// 判断当前数据是否已经是全部数据
			if(this.params.pagenum*this.params.pagesize>=this.total){
				return uni.$showMsg('到底拉')
			}
			if(this.isLoading)return
	    	// 让页数加1
			this.params.pagenum++
			this.getGoodsList()
	    },
		methods: {
			clickaa(goods){
				uni.navigateTo({
					url:'/subpkg/goods-detail/goods-detail?goods_id='+goods.goods_id
				})
			},
			// 获取商品列表
			async getGoodsList(fn) {
				this.isLoading = true
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/goods/search', this.params)
				this.isLoading = false
				// console.log(res)
				// 关闭下拉刷新 判断是不是在下拉刷新中调用 如果是关闭
				fn && fn()
				if (meta.status !== 200) return uni.$showMsg()
				console.log(message.goods);
				this.goodsList = [...this.goodsList, ...message.goods]
				this.total = message.total

			},
		},

	}
</script>

<style lang="scss">

</style>
